#include "stdafx.h"
#include "../sgp/ClipRectangle.h"

namespace ja2{ namespace sgp
{
	ClipRectangle::ClipRectangle(Rect const &Rect_)
		: m_Rect(Rect_)
	{
	}

	void ClipRectangle::clip(std::uint16_t &X1, std::uint16_t &Y1, std::uint16_t &X2, std::uint16_t &Y2)
	{
		// Clip only if partial
		if(get_clip(X1, Y1 ,X2, Y2) == CT_PARTIAL)
		{
			if( X1 < m_Rect.left())
				X1 = m_Rect.left();
			if(X2 > m_Rect.right())
				X2 = m_Rect.right();
			if(Y1 < m_Rect.top())
				Y1 = m_Rect.top();
			if(Y2 > m_Rect.bottom())
				Y2 = m_Rect.bottom();
		}
	}

	ClipRectangle::ClipType ClipRectangle::get_clip(std::uint16_t X1, std::uint16_t Y1, std::uint16_t X2, std::uint16_t Y2)
	{
		// No clipping at all
		if(X1 >= m_Rect.left() && X2 <= m_Rect.right() && Y1 >= m_Rect.top() && Y2 <= m_Rect.bottom())
			return CT_NO;
		// Full clipping
		if(X1 > m_Rect.right() || X2 < m_Rect.left() || Y1 > m_Rect.bottom() || Y2 < m_Rect.top())
			return CT_FULL;
		// Partial
		return CT_PARTIAL;
	}
} /*sgp*/} /*ja2*/